import { message } from 'antd';
import {
  emailSave,
  emailUpdate,
  notifySave,
  notifyUpdate,
  getNotifyDetail,
  getEmailDetail,
} from '@/services/templatesettings';
import router from 'umi/router';

export default {
  state: { parameter: {} },
  effects: {
    // 邮件新增
    *fetchEmailSave({ payload }, { call, put, select }) {
      yield put({
        type: 'parametering',
        payload,
      });

      const { parameter, vno, emlMsgTemplateId } = yield select(state => state.templatesettings);
      const response = emlMsgTemplateId
        ? yield call(emailUpdate, { ...parameter, emlMsgTemplateId, vno })
        : yield call(emailSave, parameter);
      if (response.success) {
        if (response.data) {
          message.warning('温馨提示：您配置的地区与合同类型已存在');
        } else {
          router.goBack();
        }
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 入职告知新增
    *fetchNotifySave({ payload }, { call, select, put }) {
      yield put({
        type: 'parameteringNotify',
        payload,
      });
      const { entryInformId, vno, parameter } = yield select(state => state.templatesettings);
      const response = entryInformId
        ? yield call(notifyUpdate, { ...parameter, entryInformId, vno })
        : yield call(notifySave, parameter);
      if (response.success) {
        if (response.data) {
          message.warning('温馨提示：您配置的地区与材料类型已存在');
        } else {
          router.goBack();
        }
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 邮件详情
    *fetchEmailDetail({ payload }, { call, put }) {
      const response = yield call(getEmailDetail, payload);
      if (response.success) {
        yield put({
          type: 'getEmailDetail',
          payload: response.data,
        });
        return response.data;
      }
      return message.warning(response.message || '服务端错误');
    },
    // 入职告知详情
    *fetchNotifyDetail({ payload }, { call, put }) {
      const response = yield call(getNotifyDetail, payload);
      if (response.success) {
        yield put({
          type: 'getNotifyDetail',
          payload: response.data,
        });
        return response.data;
      }
      return message.warning(response.message || '服务端错误');
    },
  },
  reducers: {
    getEmailDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getNotifyDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    fetchNotifyDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 入职告知数据处理
    parameteringNotify(state, { payload }) {
      let pdFlag;
      if (payload.isOpenProSet) {
        payload.pdLists.forEach(item => {
          const newitem = item;
          const pdIdList = [];
          newitem.productIdList = item.entryInformAsscId
            ? payload.values[`productId_${item.entryInformAsscId}`]
            : payload.values[`productId_${item.id}`];
          newitem.informContent = item.entryInformAsscId
            ? payload.values[`informContent_${item.entryInformAsscId}`]
            : payload.values[`informContent_${item.id}`];

          newitem.productIdList.forEach(itm => {
            pdIdList.push(itm.key);
          });
          newitem.pdIds = pdIdList;
          delete newitem.id;
          delete newitem.productIdList;
          delete newitem.pdId;
          delete newitem.pdName;
          delete newitem.entryInformId;
          delete newitem.entryInformAsscId;
        });
        pdFlag = 1;
      } else {
        pdFlag = 2;
      }
      const areaArr = [];
      const comtArr = [];
      Object.keys(payload.values).forEach(key => {
        if (key === 'areaIds' && payload.values[key]) {
          payload.values[key].forEach(id => {
            areaArr.push(id.key);
          });
        }
        if (key === 'contTypes' && payload.values[key]) {
          payload.values[key].forEach(id => {
            comtArr.push(id.key);
          });
        }
      });
      const parameter = {
        asscList: payload.pdLists,
        areaIds: areaArr,
        contTypes: comtArr,
        informContent: payload.values.informContent,
        remark: payload.values.remark,
        pdFlag,
        state: 1,
      };
      return {
        ...state,
        parameter,
      };
    },
    // 邮件模板数据处理
    parametering(state, { payload }) {
      let pdFlag;
      if (payload.isOpenProSet) {
        payload.pdLists.forEach(item => {
          const newitem = item;
          newitem.productIdList = item.entryInformAsscId
            ? payload.values[`productId_${item.entryInformAsscId}`]
            : payload.values[`productId_${item.id}`];
          newitem.emlContent = item.entryInformAsscId
            ? payload.values[`emlContent_${item.entryInformAsscId}`]
            : payload.values[`emlContent_${item.id}`];
          newitem.msgContent = item.entryInformAsscId
            ? payload.values[`msgContent_${item.entryInformAsscId}`]
            : payload.values[`msgContent_${item.id}`];
          newitem.emlTheme = item.entryInformAsscId
            ? payload.values[`emlTheme_${item.entryInformAsscId}`]
            : payload.values[`emlTheme_${item.id}`];
          const pdIdList = [];
          newitem.productIdList.forEach(itm => {
            pdIdList.push(itm.key);
          });
          newitem.pdIds = pdIdList;
          delete newitem.id;
          delete newitem.productIdList;
          delete newitem.pdId;
          delete newitem.pdName;
          delete newitem.entryInformAsscId;
          delete newitem.emlMsgTemplateId;
        });
        pdFlag = 1;
      } else {
        pdFlag = 2;
      }
      const areaArr = [];
      const comtArr = [];
      Object.keys(payload.values).forEach(key => {
        if (key === 'areaIds' && payload.values[key]) {
          payload.values[key].forEach(id => {
            areaArr.push(id.key);
          });
        }
        if (key === 'contTypes' && payload.values[key]) {
          payload.values[key].forEach(id => {
            comtArr.push(id.key);
          });
        }
      });
      const parameter = {
        emlMsgTemplateAsscVos: payload.pdLists,
        areaIds: areaArr,
        contTypes: comtArr,
        msgContent: payload.values.msgContent,
        emlTheme: payload.values.emlTheme,
        emlContent: payload.values.emlContent,
        remark: payload.values.remark,
        pdFlag,
        state: 1,
      };
      return {
        ...state,
        parameter,
      };
    },
  },
};
